package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.mapper.HSminiProgramMapper;
import com.dc.business.datacollectbusiness.model.HSminiProgramData;
import com.dc.business.datacollectbusiness.model.HSminiProgramResult;
import com.dc.business.datacollectbusiness.service.HSminiProgramService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class HSminiProgramServiceImpl implements HSminiProgramService {

    @Autowired
    private HSminiProgramMapper hsminiprogramMapper;

    @Override
    public List<HSminiProgramResult> dataSelect(String startDate, String endDate, String collegeAndPoint) {
        String point = null;
        Integer mult = 1;
        String channelno = "1";
        HashMap<String, String> map = getPointNum();
        HashMap<String, Integer> pointMult = getPointMult();
        for (String key : map.keySet()) {
             if (collegeAndPoint.equals(key)) {
                 point = map.get(key);
                 mult = pointMult.get(key);
                 channelno = getPointChannel().get(key);
             }
        }
        if (point == null) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime date = LocalDateTime.parse(startDate + "T00:00:00", DateTimeFormatter.ISO_DATE_TIME);
        // 减去一天
        LocalDateTime previousDay = date.minusDays(1);
        startDate = previousDay.format(formatter);
        startDate += " 23:00:00";
        endDate += " 23:59:59";


        List<HSminiProgramData> hSminiProgramDataList = hsminiprogramMapper.dataSelect(startDate, endDate, point, channelno);

        List<HSminiProgramResult> hSminiProgramResultList = new ArrayList<>();
        for (int i = 0; i < hSminiProgramDataList.size() - 1; i++) {
            HSminiProgramResult hSminiProgramResult = new HSminiProgramResult();
            hSminiProgramResult.setCollectTime(hSminiProgramDataList.get(i).getUploadTime());
            hSminiProgramResult.setOnline(true);
            if(i == 0){
                LocalDateTime currentTime = LocalDateTime.now();
                Duration duration = Duration.between(currentTime, hSminiProgramDataList.get(i).getUploadTime());
                double timeDifferenceInHours = duration.toHours();
                if(timeDifferenceInHours >= 2){
                    HSminiProgramResult hSminiProgramResult1 = new HSminiProgramResult();
                    hSminiProgramResult1.setOnline(false);
                    hSminiProgramResult1.setCollectTime(currentTime);
                    hSminiProgramResult1.setAllNum(0.0);
                    hSminiProgramResult1.setCostNum(0.0);
                    hSminiProgramResultList.add(hSminiProgramResult1);
                }
            }

            hSminiProgramResult.setAllNum( Math.abs(Double.parseDouble(hSminiProgramDataList.get(i).getPv())) / mult);
            hSminiProgramResult.setCostNum( Math.abs(Double.parseDouble(hSminiProgramDataList.get(i + 1).getPv()) - Double.parseDouble(hSminiProgramDataList.get(i).getPv())) / mult );

            hSminiProgramResultList.add(hSminiProgramResult);
        }
        return hSminiProgramResultList;
    }

    @Override
    public List<HashMap<String, String>> dataBarChart(String collegeName, String timeRange) {
        List<HashMap<String, String>> reslutList = new ArrayList<>();
        HashMap<String, String> pointNum = getPointNum();
        HashMap<String, Integer> pointMult = getPointMult();
        HashMap<String, String> pointChannel = getPointChannel();

        String todayStartDate = null;
        String todayEndDate = null;
        String targetStartDate = null;
        String targetEndDate = null;

        LocalDateTime now = LocalDateTime.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = now.format(formatter);
        todayStartDate = today + " 00:00:00";
        todayEndDate = today + " 23:59:59";

        switch (timeRange) {
            case "今天" -> {
                LocalDateTime yesterday = now.minusDays(1);
                String targetDate = yesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            case "昨天" -> {
                today = now.minusDays(1).format(formatter);
                todayStartDate = today + " 00:00:00";
                todayEndDate = today + " 23:59:59";
                LocalDateTime moreYesterday = now.minusDays(2);
                String targetDate = moreYesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            case "近7天" -> {
                LocalDateTime yesterday = now.minusWeeks(1);
                String targetDate = yesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            default -> {
               return null;
            }
        }
        Double todayPv;
        Double targetPv;
        for (String key : pointNum.keySet()) {
            if (key.contains(collegeName)) {
                String point = pointNum.get(key);
                Integer mult = pointMult.get(key);
                String channel = pointChannel.get(key);

                List<HSminiProgramData> todayList = hsminiprogramMapper.dataSelect(todayStartDate, todayEndDate, point, channel);
                List<HSminiProgramData> targetList = hsminiprogramMapper.dataSelect(targetStartDate, targetEndDate, point, channel);
                if (targetList.size() == 24 && !todayList.isEmpty()) {
                    todayPv = Double.parseDouble(todayList.getLast().getPv()) / mult;
                    targetPv = Double.parseDouble(targetList.getLast().getPv()) / mult;
                }else {
                    return null;
                }

                Double resultPv = todayPv - targetPv;
                String resultPvformatte = String.format("%.2f", resultPv);
                HashMap<String, String> result = new HashMap<>();

                String[] split = key.split("-");
                String neoKey = split[1];

                result.put(neoKey, resultPvformatte);
                reslutList.add(result);
            }
        }
        return reslutList;
    }

    @Override
    public List<HashMap<String, String>> getAllWaterorElec(String timeRange) {
        List<HashMap<String, String>> reslutList = new ArrayList<>();
        HashMap<String, String> pointNum = getPointNum();
        HashMap<String, Integer> pointMult = getPointMult();
        HashMap<String, String> pointChannel = getPointChannel();

        String todayStartDate = null;
        String todayEndDate = null;
        String targetStartDate = null;
        String targetEndDate = null;

        LocalDateTime now = LocalDateTime.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = now.format(formatter);
        todayStartDate = today + " 00:00:00";
        todayEndDate = today + " 23:59:59";

        switch (timeRange) {
            case "今天" -> {
                LocalDateTime yesterday = now.minusDays(1);
                String targetDate = yesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            case "昨天" -> {
                today = now.minusDays(1).format(formatter);
                todayStartDate = today + " 00:00:00";
                todayEndDate = today + " 23:59:59";
                LocalDateTime moreYesterday = now.minusDays(2);
                String targetDate = moreYesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            case "近7天" -> {
                LocalDateTime yesterday = now.minusWeeks(1);
                String targetDate = yesterday.format(formatter);
                targetStartDate = targetDate + " 00:00:00";
                targetEndDate = targetDate + " 23:59:59";
            }
            default -> {
                return null;
            }
        }
        Double waterResult = 0.0;
        Double ElecResult = 0.0;
        for (String key : pointNum.keySet()) {
            String point = pointNum.get(key);
            Integer mult = pointMult.get(key);
            String channel = pointChannel.get(key);

            if (!key.contains("全校电表")) {
                List<HSminiProgramData> todayList = hsminiprogramMapper.dataSelect(todayStartDate, todayEndDate, point, channel);
                List<HSminiProgramData> targetList = hsminiprogramMapper.dataSelect(targetStartDate, targetEndDate, point, channel);
                if (targetList.size() == 24 && !todayList.isEmpty()) {
                    Double wPv = Math.abs(Double.parseDouble(todayList.getLast().getPv()) / mult - Double.parseDouble(targetList.getLast().getPv()) / mult);
                    waterResult += wPv;
                }
            } else {
                List<HSminiProgramData> todayList = hsminiprogramMapper.dataSelect(todayStartDate, todayEndDate, point, channel);
                List<HSminiProgramData> targetList = hsminiprogramMapper.dataSelect(targetStartDate, targetEndDate, point, channel);
                if (targetList.size() == 24 && !todayList.isEmpty()) {
                    Double ePv = Math.abs(Double.parseDouble(todayList.getLast().getPv()) / mult - Double.parseDouble(targetList.getLast().getPv()) / mult);
                    ElecResult += ePv;
                }
            }
        }
        HashMap<String, String> map1 = new HashMap<>();
        String pv1 = String.format("%.2f", waterResult);
        pv1 = "周平安大傻逼" + pv1;
        map1.put("全校用水",pv1);
        HashMap<String, String> map2 = new HashMap<>();
        String pv2 = String.format("%.2f", ElecResult);
        pv2 = "周平安大傻逼" + pv2;
        map2.put("全校用电",pv2);
        reslutList.add(map1);
        reslutList.add(map2);
        return reslutList;
    }

    private HashMap<String,String> getPointNum(){
        HashMap<String,String> map1 = new HashMap<>();
        map1.put("全校电表-KSD000001","704100021");
        map1.put("全校电表-KSD000002","704100011");
        map1.put("健体斋-卫生间","404120121");
        map1.put("健体斋-水房右侧","404120111");
        map1.put("健体斋-水房左侧","404120011");
        map1.put("健体斋-洗衣机房右侧","404120141");
        map1.put("健体斋-洗衣机房左侧","404120131");
        map1.put("外国语马克思学院-1楼中水-DN65","404070762");
        map1.put("外国语马克思学院-1楼自来水-DN32","404070842");
        map1.put("外国语马克思学院-3楼中水-DN65","404070762");
        map1.put("外国语马克思学院-3楼自来水-DN32","404070842");
        map1.put("学智斋-卫生间","404120501");
        map1.put("学智斋-水房右侧","404120491");
        map1.put("学智斋-水房左侧","404120481");
        map1.put("建筑学院-1楼中水-DN50","404070961");
        map1.put("建筑学院-1楼自来水-DN32","404070971");
        map1.put("建筑学院-3楼中水-DN50","404070981");
        map1.put("建筑学院-3楼自来水-DN32","404070991");
        map1.put("控制学院-1楼中水-DN65","404070901");
        map1.put("控制学院-1楼自来水-DN50","404070911");
        map1.put("控制学院-3楼中水-DN65","404070772");
        map1.put("控制学院-3楼自来水-DN50","404070772");
        map1.put("艺术学院-1楼中水-DN65","404070862");
        map1.put("艺术学院-1楼自来水-DN40","404070852");
        map1.put("艺术学院-3楼中水-DN65","404070862");
        map1.put("艺术学院-3楼自来水-DN40","404070852");
        map1.put("计算机中心-1楼中水-DN65","404070921");
        map1.put("计算机中心-1楼自来水-DN40","404070931");
        map1.put("计算机学院主楼-1楼中水-DN80","404070941");
        map1.put("计算机学院主楼-1楼自来水-DN40","404070951");
        map1.put("计算机学院主楼-3楼中水-DN80","404070882");
        map1.put("计算机学院主楼-3楼自来水-DN50","404070882");

        return map1;
    }

    private HashMap<String,Integer> getPointMult(){
        HashMap<String,Integer> map2 = new HashMap<>();
        map2.put("全校电表-KSD000001",1);
        map2.put("全校电表-KSD000002",1);
        map2.put("健体斋-卫生间",10);
        map2.put("健体斋-水房右侧",10);
        map2.put("健体斋-水房左侧",10);
        map2.put("健体斋-洗衣机房右侧",10);
        map2.put("健体斋-洗衣机房左侧",10);
        map2.put("外国语马克思学院-1楼中水-DN65",10);
        map2.put("外国语马克思学院-1楼自来水-DN32",1000);
        map2.put("外国语马克思学院-3楼中水-DN65",10);
        map2.put("外国语马克思学院-3楼自来水-DN32",1000);
        map2.put("学智斋-卫生间",10);
        map2.put("学智斋-水房右侧",10);
        map2.put("学智斋-水房左侧",10);
        map2.put("建筑学院-1楼中水-DN50",10);
        map2.put("建筑学院-1楼自来水-DN32",1000);
        map2.put("建筑学院-3楼中水-DN50",10);
        map2.put("建筑学院-3楼自来水-DN32",1000);
        map2.put("控制学院-1楼中水-DN65",10);
        map2.put("控制学院-1楼自来水-DN50",10);
        map2.put("控制学院-3楼中水-DN65",10);
        map2.put("控制学院-3楼自来水-DN50",10);
        map2.put("艺术学院-1楼中水-DN65",10);
        map2.put("艺术学院-1楼自来水-DN40",1000);
        map2.put("艺术学院-3楼中水-DN65",10);
        map2.put("艺术学院-3楼自来水-DN40",1000);
        map2.put("计算机中心-1楼中水-DN65",10);
        map2.put("计算机中心-1楼自来水-DN40",1000);
        map2.put("计算机学院主楼-1楼中水-DN80",10);
        map2.put("计算机学院主楼-1楼自来水-DN40",1000);
        map2.put("计算机学院主楼-3楼中水-DN80",10);
        map2.put("计算机学院主楼-3楼自来水-DN50",10);

        return map2;
    }

    private HashMap<String,String> getPointChannel(){
        HashMap<String,String> map1 = new HashMap<>();
        map1.put("全校电表-KSD000001","1");
        map1.put("全校电表-KSD000002","1");
        map1.put("健体斋-卫生间","1");
        map1.put("健体斋-水房右侧","1");
        map1.put("健体斋-水房左侧","1");
        map1.put("健体斋-洗衣机房右侧","1");
        map1.put("健体斋-洗衣机房左侧","1");
        map1.put("外国语马克思学院-1楼中水-DN65","2");
        map1.put("外国语马克思学院-1楼自来水-DN32","2");
        map1.put("外国语马克思学院-3楼中水-DN65","1");
        map1.put("外国语马克思学院-3楼自来水-DN32","1");
        map1.put("学智斋-卫生间","1");
        map1.put("学智斋-水房右侧","1");
        map1.put("学智斋-水房左侧","1");
        map1.put("建筑学院-1楼中水-DN50","1");
        map1.put("建筑学院-1楼自来水-DN32","1");
        map1.put("建筑学院-3楼中水-DN50","1");
        map1.put("建筑学院-3楼自来水-DN32","1");
        map1.put("控制学院-1楼中水-DN65","1");
        map1.put("控制学院-1楼自来水-DN50","1");
        map1.put("控制学院-3楼中水-DN65","1");
        map1.put("控制学院-3楼自来水-DN50","2");
        map1.put("艺术学院-1楼中水-DN65","1");
        map1.put("艺术学院-1楼自来水-DN40","1");
        map1.put("艺术学院-3楼中水-DN65","2");
        map1.put("艺术学院-3楼自来水-DN40","2");
        map1.put("计算机中心-1楼中水-DN65","1");
        map1.put("计算机中心-1楼自来水-DN40","1");
        map1.put("计算机学院主楼-1楼中水-DN80","1");
        map1.put("计算机学院主楼-1楼自来水-DN40","1");
        map1.put("计算机学院主楼-3楼中水-DN80","1");
        map1.put("计算机学院主楼-3楼自来水-DN50","2");

        return map1;
    }
}
